/***************************************************************************
 *   File FilterLP.cpp		author belot nicolas (karrakis)            *
 *   define Class FilterLP                                                 *
 *   a low pass filters                                                    *
 ***************************************************************************
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.             *
 ***************************************************************************/

#include "FilterHP.h"
#include "math.h"
#include <stdio.h>
namespace libtrckr {
string FilterHP::HowToTune(){
	return "Spin 0 22050; Spin 0 4; NyQuist";
}
FilterHP::FilterHP(){
	_freq=0;
}

void FilterHP::Init(){

}

FilterHP::~FilterHP(){}

void FilterHP::Tune(DynArray<int> v){
//Hp take frequency parameter, the bitrate and the number of cascading filter (more than 2 cause the filter to became unstable...)
	for(unsigned int i=0;i<v.size();i++){
		printf("%d : %d\t",i,v[i]);
	}
	printf("");
	_freq=v[0];
	double tmp=double(_freq)/double(v[1]);
	if(tmp > 0.5) tmp=0.5;
	tmp = exp(-2.0 * M_PI * tmp);
	printf("%d, %g",_freq,tmp);

	DynArray<double>a(2,0.0);
	DynArray<double>b(2,1.0);	
	a[0]=(1+tmp)/2.0;
	a[1]=-(1+tmp)/2.0;
	b[1]=tmp;
	b[0]=1;
	if(v[2]==1){
		setAs(a);
		setBs(b);
	}else if(v[2]==2 ){
		Transfert t(a,b);
		Transfert t2(t);
		t*=t2;
		
		setAs(t.getAs());
		setBs(t.getBs());
	}else if(v[2]==4){
		Transfert t(a,b);
		t*=t;
		t*=t;
		setAs(t.getAs());
		setBs(t.getBs());
	}
	DynArray<double>c(getBs());
	for(unsigned int i=0;i<c.size();i++){
		c[i] = 0;
	}
	setY(c);
}

/*double* FilterLP::Preview(double* buffer, int size){
	vector <double>a = getAs();
	vector <double>b = getBs();
	double d;
	for(int i=0;i<size;i++){
		d=0.0;
		for(int j=0;j<a.size();i++){
			d+= a[i] * buffer[i-j];
		}
		
		for(int j=1;j<b.size();j++){
			d+= b[i] * _y[j];
		}
		
		for(int j=b.size()-1;j>0;j--){
			_y[i]=_y[i-1];
		}
		_y[1] = d;
		buffer[i]=d;
	}
	return buffer;
}

double* FilterLP::Execution(double *buffer, int size){
	return Preview(buffer, size);
}*/
}
